package com.ssyu.examination.exam.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.ssyu.examination.conmmon.PageModel;
import com.ssyu.examination.conmmon.util.DBUtil;
import com.ssyu.examination.exam.entity.ExamRecords;
import com.ssyu.examination.questionbank.entity.QesExamPaperEntity;
import com.ssyu.examination.system.entity.ClazzEntity;

public class ExamRecordsService {
	/**
	 * 获取班级生成下拉
	 * 
	 * @return
	 */
	public List<ClazzEntity> getClazz() {
		String sql = "SELECT id, name FROM sys_class";
		return DBUtil.executeQuery(ClazzEntity.class, sql);
	}

	/**
	 * 查询考试记录信息
	 * 
	 * @param examRecords
	 * @param page
	 * @param rows
	 * @return
	 */
	public PageModel<ExamRecords> liststatistics(ExamRecords examRecords,
			int page, int rows) {
		List<Object> paramList = new ArrayList<Object>();
		StringBuilder sb = new StringBuilder(
				"SELECT er.id,su.`name` as user__name,qr.`name` as qep__name,er.start_time,er.submit_time,qr.total_point as qep__total_point,er.get_point,er.is_pass FROM exam_records er,exam_plan ep,qes_exam_paper qr,sys_user su,sys_class sc WHERE er.user_id=su.id AND er.exam_plan_id=ep.id AND ep.exam_paper_id=qr.id AND ep.to_class_id=sc.id ");
		if (StringUtils.isNotBlank(examRecords.getUser__name())) {
			sb.append("AND su.name like ? ");
			paramList.add("%" + examRecords.getUser__name() + "%");
		}
		if (examRecords.getClazzId() != 0) {
			sb.append("AND sc.id=? ");
			paramList.add(examRecords.getClazzId());
		}
		if (StringUtils.isNotBlank(examRecords.getQep__name())) {
			sb.append("AND qr.name like ? ");
			paramList.add("%" + examRecords.getQep__name() + "%");
		}
		if (examRecords.getGetPoint1() != 0) {
			sb.append("AND er.get_point >= ? ");
			paramList.add(examRecords.getGetPoint1());
		}
		if (examRecords.getGetPoint2() != 0) {
			sb.append("AND er.get_point <= ? ");
			paramList.add(examRecords.getGetPoint2());
		}
		if (examRecords.getIsPass() != 0) {
			sb.append("AND er.is_pass = ? ");
			paramList.add(examRecords.getIsPass());
		}
		sb.append("ORDER BY er.id DESC LIMIT ?,?");
		paramList.add((page - 1) * rows);
		paramList.add(rows);
		return DBUtil.executeQueryByPage(ExamRecords.class, sb.toString(),
				paramList);
	}

	/**
	 * 开始考试
	 * 
	 * @param userId
	 * @param examPlanId
	 * @param startTime
	 * @return
	 */
	public int addStartStatistics(int userId, int examPlanId, String startTime) {
		String sql = "INSERT INTO exam_records VALUES(null,?,?,null,,null,?,3)";
		return DBUtil.executeUpdate(sql, userId, examPlanId, startTime);
	}

	/**
	 * 查询分数是否通过考试
	 * 
	 * @param getPoint
	 * @return
	 */
	public QesExamPaperEntity isPass(int getPoint) {
		String sql = "SELECT * FROM qes_exam_paper WHERE pass_point < ?";
		return DBUtil.getUniqueResult(QesExamPaperEntity.class, sql, getPoint);
	}

	public int stopStartStatistics(int userId, int examPlanId,
			String submitTime, int getPoint, int state) {
		String sql = "UPDATE exam_records SET get_point=?,submit_time=?,is_pass=? WHERE user_id=? AND exam_plan_id=?";
		return DBUtil.executeUpdate(sql, getPoint, submitTime, state, userId,
				examPlanId);
	}

}
